import com.zehua.entity.User;
import com.zehua.service.CustomerService;
import com.zehua.util.PageCommon;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zehua.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class Mytest {

    ApplicationContext ac = null;

    @Autowired
    private UserService userService = null;

    @Autowired
    private CustomerService customerService = null;

    @Before
    public void init() {
        ac = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        userService = (UserService) ac.getBean("userService");
        customerService = (CustomerService) ac.getBean("customerService");
    }

@Test
    public void testLogin() {
        User user = new User();
        user.setName("zhangsan");
        user.setPassword("201811");
        User u = userService.userLogin(user);

        System.out.println(u.getId() + "," + u.getName());
    }

    @Test
    public void testUsers() {
        List<User> list = userService.getUsers();

        for (User u : list) {
            System.out.println(u.getId() + "," + u.getName());
        }
    }


    @Test
    public void testUsercount() {
        System.out.println(userService.getUserCount());
    }

    @Test
    public void testTotalPage() {
        System.out.println(PageCommon.getTotalPages(21, 5));
    }

    @Test
    public void testPage() {
        Map<String, Integer> cond = new HashMap<>();
        cond.put("start", 5 * (1 - 1));
        cond.put("length", 5);

        List<User> list = userService.getUsersByPage(cond);
        for (User u : list) {
            System.out.println(u.getId() + "," + u.getName());
        }
    }
}
